package com.bootdo.jiashan.service.impl;

import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.BuildTree;
import com.bootdo.jiashan.model.Area;
import com.bootdo.system.domain.MenuDO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.jiashan.dao.StandardAreaDao;
import com.bootdo.jiashan.domain.StandardAreaDO;
import com.bootdo.jiashan.service.StandardAreaService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class StandardAreaServiceImpl implements StandardAreaService {
	@Autowired
	private StandardAreaDao standardAreaDao;
	
	@Override
	public StandardAreaDO get(Integer id){
		return standardAreaDao.get(id);
	}
	
	@Override
	public List<StandardAreaDO> list(Map<String, Object> map){
		return standardAreaDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return standardAreaDao.count(map);
	}
	
	@Override
	public int save(StandardAreaDO standardArea){
		return standardAreaDao.save(standardArea);
	}
	
	@Override
	public int update(StandardAreaDO standardArea){
		return standardAreaDao.update(standardArea);
	}

	@Override
	@Transactional
	public int updateByStandard(Integer standard, String[] areaCodes) {
		standardAreaDao.delByStandard(standard);

		Map<String,Area> map = Area.getAreaMap();
		if(areaCodes != null && areaCodes.length > 0){
			List<StandardAreaDO> list = new ArrayList<>();
			for (String areaCode : areaCodes) {
				StandardAreaDO areaDO = new StandardAreaDO();
				areaDO.setAreaCode(areaCode);
				areaDO.setStandard(standard);
				Area area = map.get(areaCode);
				areaDO.setArea(area.getName());
				list.add(areaDO);
			}
			standardAreaDao.batchSave(list);
		}
		return 0;
	}

	@Override
	public int remove(Integer id){
		return standardAreaDao.remove(id);
	}
	
	@Override
	public int batchRemove(Integer[] ids){
		return standardAreaDao.batchRemove(ids);
	}

	@Override
	public Tree<Area> getTree(Integer standard) {
		List<Area> areaList = Area.getAreaList();
		Map<String, Object> map = new HashMap<>();
		map.put("standard",standard);
		List<StandardAreaDO> standardAreaDOS = standardAreaDao.list(map);

		List<Tree<Area>> trees = new ArrayList<Tree<Area>>();
		for (Area area : areaList) {
			Tree<Area> tree = new Tree<Area>();
			tree.setId(area.getCode());
			tree.setParentId(area.getParentCode());
			tree.setText(area.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("selected", false);
			if(CollectionUtils.isNotEmpty(standardAreaDOS)){
				for (StandardAreaDO standardAreaDO : standardAreaDOS) {
					if(standardAreaDO.getAreaCode().equals(area.getCode())){
						state.put("selected", true);
						break;
					}
				}
			}
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<Area> t = BuildTree.build(trees);
		return t;
	}

}
